package com.learn.training.multithread.chapter6.minchapter6.demo1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        List<Thread> threads = new ArrayList<Thread>();


        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(){
                public void run(){
                    while (true){
                        try {
                            System.out.println(Thread.currentThread().getName() + "  " + Arrays.toString(counter.get()));
                        }catch (Exception e){
                            return;
                        }
                    }
                }
            };
            t.start();
            threads.add(t);
        }


        Thread getT = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                counter.increment(i);
                System.out.println(Thread.currentThread().getName() + "  increment " + i);
                try {
                    Thread.sleep(100);
                }catch (Exception e){}
            }
        });
        getT.start();
        getT.join();


        for (Thread t : threads) {
            t.interrupt();
        }
    }
}
